/**
 * @file main.c
 * @brief 
 * @author  ()
 * @version 1.0
 * @date 2024-09-13
 * 
 * @copyright Copyright (C) 2019 Queclink Wireless Solutions (ShenZhen) Co., Ltd. All Rights Reserved.
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2024-09-13 <td>1.0     <td>     <td>内容
 * </table>
 */

#include <stdio.h>

#define  LOG_TAG "MAIN"
#include "easylog/elog.h"


/* Includes ------------------------------------------------------------------*/
#include "cmsis_os2.h"
#include "bsp.h"
#include "global.h"
#include "hal_adapter.h"
#include "flash/flash_main.h"
#include "misc/version.h"
#include "misc/hwmisc.h"
#include "cm_backtrace.h"


/* Private includes ----------------------------------------------------------*/
#if defined(__ICCARM__)
#include <LowLevelIOInterface.h>
#endif /* __ICCARM__ */



/* Private function prototypes -----------------------------------------------*/
void easylogger_init(void);
void cmbacktrace_init(void);
extern void load_param_from_flash(void);

/**
 * @brief  The application entry point.
 * @retval int
 */
int main(void)
{
    /* 中断向量表重定向到APP FLASH首地址 */
    SCB->VTOR = FLASH_BASE | 0x0A000;

    /* bsp init */
    bsp_init();

    /* cmdback trace init */
    misc_read_hwver();
    cmbacktrace_init();

    /* 外部flash初始化和格式化 */
    flash_main_init();
    
    /* easylog init */
    easylogger_init();

    /* load platform param from flash */
    load_param_from_flash();

    /* Init scheduler */
    osKernelInitialize();

    /* Call init function for freertos objects (in cmsis_os2.c) */
    sailvan_bussiness_init();

    /* Start scheduler */
    osKernelStart();

    /* Infinite loop */
    while (1)
    {
    }
}

#include "FreeRTOS.h"
#include "cmsis_os2.h"
#include "cm_backtrace/cmb_def.h"
#include "cm_backtrace.h"

extern void cm_backtrace_assert(uint32_t sp);
/**
 * @brief  This function is executed in case of error occurrence.
 * @retval None
 */
void Error_Handler(void)
{
    /* USER CODE BEGIN Error_Handler_Debug */
    /* User can add his own implementation to report the HAL error return state */
    // __disable_irq();
    cm_backtrace_assert(cmb_get_sp());
    
    portDISABLE_INTERRUPTS();
	// HAL_GPIO_TogglePin(LED_RED_GPIO_Port, LED_RED_Pin);
    while (1)
    {
    }
    /* USER CODE END Error_Handler_Debug */
}

#ifdef USE_FULL_ASSERT
/**
 * @brief  Reports the name of the source file and the source line number
 *         where the assert_param error has occurred.
 * @param  file: pointer to the source file name
 * @param  line: assert_param error line source number
 * @retval None
 */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN 6 */
    /* User can add his own implementation to report the file name and line number,
       ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

void elog_fliter_init(void);
/**
 * @brief easylogger 功能初始化
 */
void easylogger_init(void)
{
    elog_init();

    elog_set_fmt(ELOG_LVL_ASSERT,  ELOG_FMT_ALL);
    elog_set_fmt(ELOG_LVL_ERROR,   ELOG_FMT_TAG | ELOG_FMT_FUNC | ELOG_FMT_LINE);
    elog_set_fmt(ELOG_LVL_WARN,    ELOG_FMT_TAG);
    elog_set_fmt(ELOG_LVL_INFO,    ELOG_FMT_TAG);
    elog_set_fmt(ELOG_LVL_DEBUG,   ELOG_FMT_TAG | ELOG_FMT_FUNC | ELOG_FMT_LINE);
    elog_set_fmt(ELOG_LVL_VERBOSE, ~ELOG_FMT_ALL);
    
    elog_start();

    elog_fliter_init();
}


#include "flash/protocol_params/protocol_params.h"
extern int32_t tbox_get_logctrl(uint8_t *enable, uint8_t *lvl, char *tag);
/**
 * @brief 日志参数过滤初始化
 */
void elog_fliter_init(void)
{
    log_params_t log = {0};

    /** 1. 读取LOG配置参数 */
    tbox_get_logctrl(&log.enable, &log.level, log.filter_tag);

    elog_set_output_enabled(log.enable);
    elog_set_filter_lvl(log.level);
    if(strlen(log.filter_tag))
    {
        elog_set_filter_tag(log.filter_tag);
    }

    log_v("setup logger config:%s, level:%d, fliter tag:%s\r\n", log.enable ? "enable":"disable", log.level, log.filter_tag);
}

void cmbacktrace_init(void)
{
#ifdef DA35D1C10_C
#ifndef NDEBUG
    cm_backtrace_init(PROJECT_NAME"-C", get_hw_ver(), SOFTWARE_VERSION);
#else
    cm_backtrace_init(SOFTWARE_VERSION, get_hw_ver(), SOFTWARE_VERSION);
#endif
#else
#ifndef NDEBUG
    cm_backtrace_init(PROJECT_NAME"-F", get_hw_ver(), SOFTWARE_VERSION);
#else
    cm_backtrace_init(SOFTWARE_VERSION, get_hw_ver(), SOFTWARE_VERSION);
#endif
#endif
}

#include "FreeRTOS.h"
#include "task.h"
void vApplicationStackOverflowHook( TaskHandle_t xTask,
                                    char *pcTaskName )
{
    printf("Stack overflow: %s\n", pcTaskName);
}

void vApplicationIdleHook (void)
{
    // printf("system idle\n");
}

